const { Controller } = require('egg')
/**
 * @Controller user
 */
const Message = require('../utils/message')
class UserController extends Controller {
  /**
   * @Router POST /user
   */
  async login () {
    const { ctx, service, app } = this
    ctx.validate({
      username: { type: 'string' },
      password: { type: 'string' }
    })
    let account = await service.user.login(ctx.request.body);
    const token = app.jwt.sign(
      { ...account },
      app.config.jwt.secret,
      { expiresIn: '60s' }
    )
    ctx.body = new Message(200, '登录成功', { token })
  }

}

module.exports = UserController